package com.region.integral.common_user.obtain.limit;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import com.region.integral.common_user.obtain.vo.IntegralData;
import com.region.integral.common_user.pojo.LimitEntity;
import com.region.integral.exception.IntegralException;
import com.region.integral.hibernate.BaseHibernate;
import com.region.integral.util.CommonUserLogUtil;
import com.region.integral.util.XMLIntegralLimitUtil;

@Aspect
@Component
public class IntegralObtainLimit extends BaseHibernate{
	@Around(value="execution(* com.region.integral.common_user.obtain.dao.impl.*.*(..)) ")
	public void execute(ProceedingJoinPoint pjp){
		String target = pjp.getSignature().getName();
		if("commentGoods".equals(target)){
			commentGoods(pjp);
		}
		if("authentication".equals(target)){
			authentication(pjp);
		}
		if("commentWeiBo".equals(target)){
			commentWeiBo(pjp);
		}
		if("groupPeopleRise".equals(target)){
			groupPeopleRise(pjp);
		}
		if("inviteFriend".equals(target)){
			inviteFriend(pjp);
		}
		if("issueActivity".equals(target)){
			issueActivity(pjp);
		}
		if("joinActivity".equals(target)){
			joinActivity(pjp);
		}
		if("joinGroup".equals(target)){
			joinGroup(pjp);
		}
		if("replyComment".equals(target)){
			replyComment(pjp);
		}
		if("sendWeiBo".equals(target)){
			sendWeiBo(pjp);
		}
		else{
			try {
				pjp.proceed(pjp.getArgs());
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}
		
	}
	private void commentGoods(ProceedingJoinPoint pjp){
		Object[] oldParms = pjp.getArgs();
		if(oldParms[0] instanceof Integer){
			int userid = (Integer)oldParms[0];
			try {
				LimitEntity limit = getLimitEntity(userid);
				Date last = limit.getLastCommentGoods();
				Date now = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				String lastStr = sdf.format(last);
				String nowStr = sdf.format(now);
				
				int entityInt = limit.getCommentGoodsInt();
				LimitRegulation lRegulation = XMLIntegralLimitUtil.unmarshaller();
				int ruleInt = lRegulation.getCommentGoods();
				if(nowStr.equals(lastStr)){
					if(entityInt < ruleInt){
						pjp.proceed(oldParms);
						Object[] newParms = pjp.getArgs();
						for(Object obj:newParms){
							if(obj instanceof IntegralData){
								int oi = ((IntegralData)obj).getObtainIntegral();
								limit.setCommentGoodsInt(oi+entityInt);
								hibernateTemplate.update(limit);
								hibernateTemplate.save(CommonUserLogUtil.log(userid, oi, "commentGoods"));
							}
						}
					}
				}
				else{
					pjp.proceed(oldParms);
					Object[] newParms = pjp.getArgs();
					for(Object obj:newParms){
						if(obj instanceof IntegralData){
							int oi = ((IntegralData)obj).getObtainIntegral();
							limit.setCommentGoodsInt(oi);
							limit.setLastCommentGoods(new Date());
							hibernateTemplate.update(limit);
							hibernateTemplate.save(CommonUserLogUtil.log(userid, oi, "commentGoods"));
						}
					}
				}
			} catch (IntegralException e) {
				e.printStackTrace();
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}
		
	}
	private void authentication(ProceedingJoinPoint pjp){
		Object[] oldParms = pjp.getArgs();
		if(oldParms[0] instanceof Integer){
			int userid = (Integer)oldParms[0];
			try {
				LimitEntity limit = getLimitEntity(userid);
				Date last = limit.getLastAuth();
				Date now = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				String lastStr = sdf.format(last);
				String nowStr = sdf.format(now);
				
				int entityInt = limit.getAuthInt();
				LimitRegulation lRegulation = XMLIntegralLimitUtil.unmarshaller();
				int ruleInt = lRegulation.getAuthentication();
				if(nowStr.equals(lastStr)){
					if(entityInt < ruleInt){
						pjp.proceed(oldParms);
						Object[] newParms = pjp.getArgs();
						for(Object obj:newParms){
							if(obj instanceof IntegralData){
								int oi = ((IntegralData)obj).getObtainIntegral();
								limit.setAuthInt(oi+entityInt);
								hibernateTemplate.update(limit);
								hibernateTemplate.save(CommonUserLogUtil.log(userid, oi, "authentication"));
							}
						}
					}
				}
				else{
					pjp.proceed(oldParms);
					Object[] newParms = pjp.getArgs();
					for(Object obj:newParms){
						if(obj instanceof IntegralData){
							int oi = ((IntegralData)obj).getObtainIntegral();
							limit.setAuthInt(oi);
							limit.setLastAuth(new Date());
							hibernateTemplate.update(limit);
							hibernateTemplate.save(CommonUserLogUtil.log(userid, oi, "authentication"));
						}
					}
				}
			} catch (IntegralException e) {
				e.printStackTrace();
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}
		
	}
	private void commentWeiBo(ProceedingJoinPoint pjp){
		Object[] oldParms = pjp.getArgs();
		if(oldParms[0] instanceof Integer){
			int userid = (Integer)oldParms[0];
			try {
				LimitEntity limit = getLimitEntity(userid);
				Date last = limit.getLastCommentWeiBo();
				Date now = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				String lastStr = sdf.format(last);
				String nowStr = sdf.format(now);
				
				int entityInt = limit.getCommentWeiBoInt();
				LimitRegulation lRegulation = XMLIntegralLimitUtil.unmarshaller();
				int ruleInt = lRegulation.getCommentWeiBo();
				if(nowStr.equals(lastStr)){
					if(entityInt < ruleInt){
						pjp.proceed(oldParms);
						Object[] newParms = pjp.getArgs();
						for(Object obj:newParms){
							if(obj instanceof IntegralData){
								int oi = ((IntegralData)obj).getObtainIntegral();
								limit.setCommentWeiBoInt(oi+entityInt);
								hibernateTemplate.update(limit);
								hibernateTemplate.save(CommonUserLogUtil.log(userid, oi, "commentWeiBo"));
							}
						}
					}
				}
				else{
					pjp.proceed(oldParms);
					Object[] newParms = pjp.getArgs();
					for(Object obj:newParms){
						if(obj instanceof IntegralData){
							int oi = ((IntegralData)obj).getObtainIntegral();
							limit.setCommentWeiBoInt(oi);
							limit.setLastCommentWeiBo(new Date());
							hibernateTemplate.update(limit);
							hibernateTemplate.save(CommonUserLogUtil.log(userid, oi, "commentWeiBo"));
						}
					}
				}
			} catch (IntegralException e) {
				e.printStackTrace();
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}
		
	}
	private void groupPeopleRise(ProceedingJoinPoint pjp){
		Object[] oldParms = pjp.getArgs();
		if(oldParms[0] instanceof Integer){
			int userid = (Integer)oldParms[0];
			try {
				LimitEntity limit = getLimitEntity(userid);
				Date last = limit.getLastGroupPeopleRise();
				Date now = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				String lastStr = sdf.format(last);
				String nowStr = sdf.format(now);
				
				int entityInt = limit.getGroupPeopleRiseInt();
				LimitRegulation lRegulation = XMLIntegralLimitUtil.unmarshaller();
				int ruleInt = lRegulation.getGroupPeopleRise();
				if(nowStr.equals(lastStr)){
					if(entityInt < ruleInt){
						pjp.proceed(oldParms);
						Object[] newParms = pjp.getArgs();
						for(Object obj:newParms){
							if(obj instanceof IntegralData){
								int oi = ((IntegralData)obj).getObtainIntegral();
								limit.setGroupPeopleRiseInt(oi+entityInt);
								hibernateTemplate.update(limit);
								hibernateTemplate.save(CommonUserLogUtil.log(userid, oi, "groupPeopleRise"));
							}
						}
					}
				}
				else{
					pjp.proceed(oldParms);
					Object[] newParms = pjp.getArgs();
					for(Object obj:newParms){
						if(obj instanceof IntegralData){
							int oi = ((IntegralData)obj).getObtainIntegral();
							limit.setGroupPeopleRiseInt(oi);
							limit.setLastGroupPeopleRise(new Date());
							hibernateTemplate.update(limit);
							hibernateTemplate.save(CommonUserLogUtil.log(userid, oi, "groupPeopleRise"));
						}
					}
				}
			} catch (IntegralException e) {
				e.printStackTrace();
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}
		
	}
	private void inviteFriend(ProceedingJoinPoint pjp){
		Object[] oldParms = pjp.getArgs();
		if(oldParms[0] instanceof Integer){
			int userid = (Integer)oldParms[0];
			try {
				LimitEntity limit = getLimitEntity(userid);
				Date last = limit.getLastInviteFriend();
				Date now = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				String lastStr = sdf.format(last);
				String nowStr = sdf.format(now);
				
				int entityInt = limit.getInviteFriendInt();
				LimitRegulation lRegulation = XMLIntegralLimitUtil.unmarshaller();
				int ruleInt = lRegulation.getInviteFriend();
				if(nowStr.equals(lastStr)){
					if(entityInt < ruleInt){
						pjp.proceed(oldParms);
						Object[] newParms = pjp.getArgs();
						for(Object obj:newParms){
							if(obj instanceof IntegralData){
								int oi = ((IntegralData)obj).getObtainIntegral();
								limit.setInviteFriendInt(oi+entityInt);
								hibernateTemplate.update(limit);
								hibernateTemplate.save(CommonUserLogUtil.log(userid, oi, "inviteFriend"));
							}
						}
					}
				}
				else{
					pjp.proceed(oldParms);
					Object[] newParms = pjp.getArgs();
					for(Object obj:newParms){
						if(obj instanceof IntegralData){
							int oi = ((IntegralData)obj).getObtainIntegral();
							limit.setInviteFriendInt(oi);
							limit.setLastInviteFriend(new Date());
							hibernateTemplate.update(limit);
							hibernateTemplate.save(CommonUserLogUtil.log(userid, oi, "inviteFriend"));
						}
					}
				}
			} catch (IntegralException e) {
				e.printStackTrace();
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}
		
	}
	private void issueActivity(ProceedingJoinPoint pjp){
		Object[] oldParms = pjp.getArgs();
		if(oldParms[0] instanceof Integer){
			int userid = (Integer)oldParms[0];
			try {
				LimitEntity limit = getLimitEntity(userid);
				Date last = limit.getLastissueActivity();
				Date now = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				String lastStr = sdf.format(last);
				String nowStr = sdf.format(now);
				
				int entityInt = limit.getIssueActivityInt();
				LimitRegulation lRegulation = XMLIntegralLimitUtil.unmarshaller();
				int ruleInt = lRegulation.getIssueActivity();
				if(nowStr.equals(lastStr)){
					if(entityInt < ruleInt){
						pjp.proceed(oldParms);
						Object[] newParms = pjp.getArgs();
						for(Object obj:newParms){
							if(obj instanceof IntegralData){
								int oi = ((IntegralData)obj).getObtainIntegral();
								limit.setIssueActivityInt(oi+entityInt);
								hibernateTemplate.update(limit);
								hibernateTemplate.save(CommonUserLogUtil.log(userid, oi, "issueActivity"));
							}
						}
					}
				}
				else{
					pjp.proceed(oldParms);
					Object[] newParms = pjp.getArgs();
					for(Object obj:newParms){
						if(obj instanceof IntegralData){
							int oi = ((IntegralData)obj).getObtainIntegral();
							limit.setIssueActivityInt(oi);
							limit.setLastissueActivity(new Date());
							hibernateTemplate.update(limit);
							hibernateTemplate.save(CommonUserLogUtil.log(userid, oi, "issueActivity"));
						}
					}
				}
			} catch (IntegralException e) {
				e.printStackTrace();
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}
		
	}
	private void joinActivity(ProceedingJoinPoint pjp){
		Object[] oldParms = pjp.getArgs();
		if(oldParms[0] instanceof Integer){
			int userid = (Integer)oldParms[0];
			try {
				LimitEntity limit = getLimitEntity(userid);
				Date last = limit.getLastJoinActivity();
				Date now = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				String lastStr = sdf.format(last);
				String nowStr = sdf.format(now);
				
				int entityInt = limit.getJoinActivityInt();
				LimitRegulation lRegulation = XMLIntegralLimitUtil.unmarshaller();
				int ruleInt = lRegulation.getJoinActivity();
				if(nowStr.equals(lastStr)){
					if(entityInt < ruleInt){
						pjp.proceed(oldParms);
						Object[] newParms = pjp.getArgs();
						for(Object obj:newParms){
							if(obj instanceof IntegralData){
								int oi = ((IntegralData)obj).getObtainIntegral();
								limit.setJoinActivityInt(oi+entityInt);
								hibernateTemplate.update(limit);
								hibernateTemplate.save(CommonUserLogUtil.log(userid, oi, "joinActivity"));
							}
						}
					}
				}
				else{
					pjp.proceed(oldParms);
					Object[] newParms = pjp.getArgs();
					for(Object obj:newParms){
						if(obj instanceof IntegralData){
							int oi = ((IntegralData)obj).getObtainIntegral();
							limit.setJoinActivityInt(oi);
							limit.setLastJoinActivity(new Date());
							hibernateTemplate.update(limit);
							hibernateTemplate.save(CommonUserLogUtil.log(userid, oi, "joinActivity"));
						}
					}
				}
			} catch (IntegralException e) {
				e.printStackTrace();
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}
		
	}
	private void joinGroup(ProceedingJoinPoint pjp){
		Object[] oldParms = pjp.getArgs();
		if(oldParms[0] instanceof Integer){
			int userid = (Integer)oldParms[0];
			try {
				LimitEntity limit = getLimitEntity(userid);
				Date last = limit.getLastJoinGroup();
				Date now = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				String lastStr = sdf.format(last);
				String nowStr = sdf.format(now);
				
				int entityInt = limit.getJoinGroupInt();
				LimitRegulation lRegulation = XMLIntegralLimitUtil.unmarshaller();
				int ruleInt = lRegulation.getJoinGroup();
				if(nowStr.equals(lastStr)){
					if(entityInt < ruleInt){
						pjp.proceed(oldParms);
						Object[] newParms = pjp.getArgs();
						for(Object obj:newParms){
							if(obj instanceof IntegralData){
								int oi = ((IntegralData)obj).getObtainIntegral();
								limit.setJoinGroupInt(oi+entityInt);
								hibernateTemplate.update(limit);
								hibernateTemplate.save(CommonUserLogUtil.log(userid, oi, "joinGroup"));
							}
						}
					}
				}
				else{
					pjp.proceed(oldParms);
					Object[] newParms = pjp.getArgs();
					for(Object obj:newParms){
						if(obj instanceof IntegralData){
							int oi = ((IntegralData)obj).getObtainIntegral();
							limit.setJoinGroupInt(oi);
							limit.setLastJoinGroup(new Date());
							hibernateTemplate.update(limit);
							hibernateTemplate.save(CommonUserLogUtil.log(userid, oi, "joinGroup"));
						}
					}
				}
			} catch (IntegralException e) {
				e.printStackTrace();
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}
		
	}	
	private void replyComment(ProceedingJoinPoint pjp){
		Object[] oldParms = pjp.getArgs();
		if(oldParms[0] instanceof Integer){
			int userid = (Integer)oldParms[0];
			try {
				LimitEntity limit = getLimitEntity(userid);
				Date last = limit.getLastReplyComment();
				Date now = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				String lastStr = sdf.format(last);
				String nowStr = sdf.format(now);
				
				int entityInt = limit.getReplyCommentInt();
				LimitRegulation lRegulation = XMLIntegralLimitUtil.unmarshaller();
				int ruleInt = lRegulation.getReplyComment();
				if(nowStr.equals(lastStr)){
					if(entityInt < ruleInt){
						pjp.proceed(oldParms);
						Object[] newParms = pjp.getArgs();
						for(Object obj:newParms){
							if(obj instanceof IntegralData){
								int oi = ((IntegralData)obj).getObtainIntegral();
								limit.setReplyCommentInt(oi+entityInt);
								hibernateTemplate.update(limit);
								hibernateTemplate.save(CommonUserLogUtil.log(userid, oi, "replyComment"));
							}
						}
					}
				}
				else{
					pjp.proceed(oldParms);
					Object[] newParms = pjp.getArgs();
					for(Object obj:newParms){
						if(obj instanceof IntegralData){
							int oi = ((IntegralData)obj).getObtainIntegral();
							limit.setReplyCommentInt(oi);
							limit.setLastReplyComment(new Date());
							hibernateTemplate.update(limit);
							hibernateTemplate.save(CommonUserLogUtil.log(userid, oi, "replyComment"));
						}
					}
				}
			} catch (IntegralException e) {
				e.printStackTrace();
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}
		
	}	
	private void sendWeiBo(ProceedingJoinPoint pjp){
		Object[] oldParms = pjp.getArgs();
		if(oldParms[0] instanceof Integer){
			int userid = (Integer)oldParms[0];
			try {
				LimitEntity limit = getLimitEntity(userid);
				Date last = limit.getLastSendWeiBo();
				Date now = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				String lastStr = sdf.format(last);
				String nowStr = sdf.format(now);
				
				int entityInt = limit.getSenWeiBoInt();
				LimitRegulation lRegulation = XMLIntegralLimitUtil.unmarshaller();
				int ruleInt = lRegulation.getSendWeiBo();
				if(nowStr.equals(lastStr)){
					if(entityInt < ruleInt){
						pjp.proceed(oldParms);
						Object[] newParms = pjp.getArgs();
						for(Object obj:newParms){
							if(obj instanceof IntegralData){
								int oi = ((IntegralData)obj).getObtainIntegral();
								limit.setSenWeiBoInt(oi+entityInt);
								hibernateTemplate.update(limit);
								hibernateTemplate.save(CommonUserLogUtil.log(userid, oi, "sendWeiBo"));
							}
						}
					}
				}
				else{
					pjp.proceed(oldParms);
					Object[] newParms = pjp.getArgs();
					for(Object obj:newParms){
						if(obj instanceof IntegralData){
							int oi = ((IntegralData)obj).getObtainIntegral();
							limit.setSenWeiBoInt(oi);
							limit.setLastSendWeiBo(new Date());
							hibernateTemplate.update(limit);
							hibernateTemplate.save(CommonUserLogUtil.log(userid, oi, "sendWeiBo"));
						}
					}
				}
			} catch (IntegralException e) {
				e.printStackTrace();
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}
		
	}
}
